//
//  QWMineInfoView.m
//  qwm
//
//  Created by kevin on 2023/3/24.
//

#import "QWMineInfoView.h"

@interface QWMineInfoView ()
@property (nonatomic, strong) UIView *loginedView;
@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic, strong) UIImageView *nextImageView;
@property (nonatomic, strong) UILabel *logRegLabel;
@property (nonatomic, strong) UIButton *logRegButtun;

@end

@implementation QWMineInfoView

-(instancetype)init
{
    self =[super init];
    if (self) {
        self.backgroundColor = Color_Main_Green;
        [self loginedView];
        [self logRegButtun];
        
        [self loginStateChangeNotification];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginStateChangeNotification) name:QWLogin_Change_State object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(qaUserInfoStateChangeNotification) name:QWUserInfo_Change_State object:nil];
    }
    return self;
}

#pragma mark - 已登录信息
- (UIView *)loginedView{
    if(!_loginedView){
        _loginedView = [[UIView alloc]init];
        _loginedView.backgroundColor = [UIColor clearColor];
        _loginedView.hidden = YES;
        [self addSubview:_loginedView];
        
        [_loginedView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
        }];
        
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickLoginedView)];
        [_loginedView addGestureRecognizer:singleTap];
        
        [self headImageView];
        [self logRegLabel];
        [self nextImageView];
    }
    return _loginedView;
}
-  (void)clickLoginedView{
    if(self.mineInfoBlock) self.mineInfoBlock(YES);
}

#pragma mark - 头像
- (UIImageView *)headImageView{
    if(!_headImageView){
        _headImageView = [[UIImageView alloc]init];
        _headImageView.tintColor = [UIColor whiteColor];
//        NSString *shichenName=[[QWCommonMethod Instance] calculateOneOfTheTwelveHourPeriods];
//        NSString *imageNameStr =[NSString stringWithFormat:@"qw_%@.png",shichenName];
//        UIImage *image =[UIImage imageNamed:imageNameStr];
//        _headImageView.image =image;
        _headImageView.layer.masksToBounds = YES;
        _headImageView.layer.cornerRadius = 40.0;
        [_loginedView addSubview:_headImageView];
        
        [_headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_loginedView);
            make.bottom.equalTo(_loginedView);
            make.left.equalTo(_loginedView);
            make.width.mas_equalTo(80);
            make.height.mas_equalTo(80);
        }];
    }
    return _headImageView;
}

#pragma mark - 用户名
- (UILabel *)logRegLabel{
    if(!_logRegLabel){
        _logRegLabel = [[UILabel alloc]init];
        _logRegLabel.text =@"xxx";
        _logRegLabel.font =[UIFont boldSystemFontOfSize:24];
        _logRegLabel.textColor =[UIColor whiteColor];
        
        [_loginedView addSubview:_logRegLabel];
        
        [_logRegLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_loginedView);
            make.left.equalTo(_headImageView.mas_right).offset(8);
        }];
    }
    return _logRegLabel;
}

#pragma mark - next
- (UIImageView *)nextImageView{
    if(!_nextImageView){
        _nextImageView = [[UIImageView alloc]init];
        UIImage *image =[UIImage imageNamed:@"next_white.png"];
        _nextImageView.image =image;
        [_loginedView addSubview:_nextImageView];
        
        [_nextImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_loginedView);
            make.right.equalTo(_loginedView);
        }];
    }
    return _nextImageView;
}

-(UIButton *)logRegButtun
{
    if (!_logRegButtun) {
        _logRegButtun =[UIButton buttonWithType:UIButtonTypeCustom];
        [_logRegButtun setTitle:@"登陆/注册" forState:UIControlStateNormal];
        [_logRegButtun setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _logRegButtun.titleLabel.font = [UIFont systemFontOfSize:24];
        _logRegButtun.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _logRegButtun.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 6);
        [_logRegButtun addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_logRegButtun];
        
        [_logRegButtun mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.mas_equalTo(20);
            make.width.equalTo(self).offset(-40);
        }];
    }
    return _logRegButtun;
}
-(void)buttonClick:(UIButton *)sender
{
    if(self.mineInfoBlock){
        self.mineInfoBlock(NO);
    }
}


#pragma mark - 接受通知

- (void)loginStateChangeNotification {
 
//    NSLog(@"接受通知==%@",AppProfile.qaUserInfo);
    
    if(AppProfile.qaUserInfo){
        self.loginedView.hidden =NO;
        self.logRegButtun.hidden = YES;
        
        QWqaUserModel *qaUserInfo = AppProfile.qaUserInfo;
        self.logRegLabel.text = qaUserInfo.name;
        
        NSString *url = [QWM_BASE_API_URL stringByAppendingFormat:@"/bokeupload%@",qaUserInfo.photo];
        [_headImageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"ic_header_default.png"]];
    }else{
        self.loginedView.hidden = YES;
        self.logRegButtun.hidden = NO;
    }
}
- (void)qaUserInfoStateChangeNotification{
    if(AppProfile.qaUserInfo){
        QWqaUserModel *qaUserInfo = AppProfile.qaUserInfo;
        self.logRegLabel.text = qaUserInfo.name;
        
        NSString *url = [QWM_BASE_API_URL stringByAppendingFormat:@"/bokeupload%@",qaUserInfo.photo];
        [_headImageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"ic_header_default.png"]];
    }
}


- (void)dealloc{
    //移除观察者 self
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
